<?php
namespace c\info;

/**
 * Description of info
 *
 * @author YeFng
 */
class a extends \m\ad
{
    var $table = 'info';
    var $auth_m = 'info';
    var $State = array('hide','publish');   

    function __construct() {
        parent::__construct();
        
        $this->FM = \YF_f::initC('\c\file\map', '', '', 'info');
        $this->SM = \YF_f::initC('\c\sort\map', '', 'info_sort_map');
        $this->title = \lang::g('Info');
    }
    
    function index_home()
    {
        $this->PU->check($this->auth_m);
        $this->loadTemplate('info/home');
    }
    // list
    function index_list_a()
    {
        $this->PU->check($this->auth_m);    
        
        $s = $this->P_G('s');
        $where = '';
        $field = '*';
        $tables = array('t1'=>array('as'=>$this->table));
        if ($s['name'])
         {
            $n = $this->db->escape($s['name']);
            $where .= " AND( title LIKE '%$n%' or description LIKE '%$n%')";
         }
        if ($s['sort_id'])
         {
            $tables['sm'] = array('as'=>'info_sort_map','join'=>'LEFT JOIN','on'=>'sm.data_id=t1.id');
            if ($s['sort_id']=='null')
                $where .= ' AND ISNULL(sm.sort_id)';
            else{
                $s_id = intval($s['sort_id']);
                $where .= " AND sm.sort_id IN($s_id)";
                $tables['sm']['join'] = 'RIGHT JOIN';
                $field .=',data_id id';
             }
         }
        if ($s['state']!='')
            $where .= ' AND state='. intval($s['state']);
        
        $order = $_GET['LO'];
        if ($order['col'])
         {
            $way = ($order['way']=='ASC' ? ' ASC' : ' DESC');
            $_A = array('id', 'hits', 'create_time', 'title');
            $order = in_array($order['col'], $_A) ? $order['col'] . $way :'';
         }
        else
            $order = 't1.id DESC';
       
        
        $where && $where = substr($where, 4);
        $res = $this->db->get_page(array(), $tables, $field, $where, $order);
        $this->loadTemplate('info/list_a', array('res'=>$res, 'state'=>\lang::Ag($this->State)));
        $html = ob_get_clean();
        
        ajax_json(array('status'=>TRUE, 'res'=>$html,
            'paging'=>$this->db->paging,
            'u_get'=>\url::p('&p&limit&LO&s[sort_id]'),
             ));
    }
    // map
    function index_sort()
    {
        $this->PU->check($this->auth_m);
        $pid = intval($_GET['id']);
        $res = $this->SM->tree($pid);
        ajax_json(array('status'=>TRUE, 'res'=>$res));
    }
    
    /**
     * 编辑
     */
    function index_post()
    {
        $this->PU->check($this->auth_m);
        $id = intval($_GET['id']);
    	if($id)
    	 {
            $this->title = \lang::g('Edit') .$this->title;
    	 	$data = $this->db->getOne($this->table, "*", 'id='.$id);
            $this->isExist($data);
            $data['arr'] = arr_decode($data['arr']);
            //正文
            $contentA = $this->db->getCol('info_content', 'content', 'info_id='. $id, 'page asc');
            $this->t_var['data_content'] = implode('<div style="page-break-after: always;"><span style="display: none;">&nbsp;</span></div>', $contentA);

            //files
            $this->t_var['file_R'] = $this->FM->get($id);
            $this->t_var['sort_R'] = $this->SM->get($id);
    	 }
    	else{
            $this->title = \lang::g('Add') .$this->title;
            $data = array( 'hits'=>0, 'state'=>1, 'arr'=>array());
            if (is_array($_GET['data'])){
                $data = array_merge($data, $_GET['data']);
             }
    	 }
        $this->t_var['data'] = $data;
        $this->loadTemplate('info/post');
        $this->dispLayout = 'sub';
    }
    function index_save()
    {
        $this->PU->check($this->auth_m);
        $p = $_POST;
        $id = intval($p['data_id']);
        $data = $p['data'];
        $content = $p['data_content'];
        
        if ($id)
		 {
			$where = "id=$id";
		 }
		else{
			$where = '';
            if (!$data['keyword'])
                $data['keyword'] = \YF_f::initC('keyword')->getKeys( $content . $data['description'] . $data['title']);
			if (!$data['description'] && $content)
				$data['description'] = mb_substr(trim(strip_tags($content)), 0, 80);
            if (!$data['title'] && $data['description'])
                $data['title'] = mb_substr(strip_tags($data['description']), 0, 10);
		 }
         
        $data['arr'] = arr_encode($data['arr']);
		$data['modified_time']= date('YmdHis');
        $b = $this->db->save($this->table, $data, $where);
		if ($b){
			if ($id){
				\log::write($this->table, 'edit', $id);
             }
			else {
				$id = $this->db->insert_id();
				\log::write($this->table, 'add', $id);
			 }
            $msg = '';
            
			//保存正文
            if ($where)
                $this->db->del('info_content', "info_id IN($id)");
			if ($content){
				$content = preg_strip_tags($content);
                
                if ($p['down_pic'])
                    $content = \YF_f::initC ('\c\img\remote')->content_match($content);
                
				$paging_rule = '/<div style="page-break-after: always[;]?">
	<span style="display: none[;]?">&nbsp;<\/span><\/div>/i';
				$contentA = preg_split($paging_rule, $content, -1, PREG_SPLIT_NO_EMPTY);
				foreach($contentA as $page=>$c)
				 {
					$b = $this->db->save('info_content', array('info_id'=>$id, 'content'=>$c, 'page'=>$page+1));
					if (!$b){
					 	$msg = \lang::g('Save content over.');
                        break;
                     }
				  }
				unset($contentA, $content);
			  }
            
            // sort
            if ($p['sortA_del'])
                $this->SM->del($id, intval_ids($p['sortA_del']));
            if (is_array($p['sortA_add']))
                $this->SM->save($id, array_unique($p['sortA_add']));
             
            //file
            if ($p['fileA'])
             {
                $fA = $p['fileA'];
                if ($fA['d'])
                    if (!$this->FM->del($id, $fA['d']))
                        $msg .= \lang::g('Del file over.');
                if ($fA['a'])
                    if (!$this->FM->add($id, $fA['a']))
                        $msg .= \lang::g('Save file over.');
             }
             
			ajax_json(array('status'=>TRUE, 'id'=>$id, 'msg'=>$msg));
		 }
        ajax_json(\lang::g('save fail')); 
    }
    function index_del()
    {
        $this->PU->check($this->auth_m);
        $ids = intval_ids($_GET['id']);
       
        if ($this->clear($ids))
         {
            $this->db->del($this->table, "id IN($ids)");
            if ($this->db->num_affected()>0)
             {
                \log::write($this->table, 'del', $ids);
                ajax_json(TRUE);	
             }            
         }

		ajax_json(\lang::g( 'Over'));
    }
    function clear($id)
    {
        $this->FM->del($id);
        $this->SM->del($id);
        $this->db->del('info_content', "info_id IN($id)");
        return true;
    }
    
    function index_removeSort()
    {
        $this->PU->check($this->auth_m);
        $id = intval_ids($_GET['id']);
        $this->SM->del($id, intval($_GET['sort_id']));
        ajax_json(TRUE);
    }
    function index_addSort()
    {
        $this->PU->check($this->auth_m);
        $ids = intval_ids($_GET['id']);
        $a = array(intval($_GET['sort_id']));
        foreach (explode(',', $ids) as $id)
            $this->SM->save($id, $a);
        ajax_json(TRUE); 
    }
}
